﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
public partial class Manage_AttoneyUpdate : System.Web.UI.Page
{
    private Int64 AccountId;
    protected void Page_Load(object sender, EventArgs e)
    {   
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAdmin );
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {

            AccountId = Int64.Parse(SysFun.IsSQL(Request.QueryString["AccountId"]));

            if (!this.IsPostBack)
            {
                HZ.MBSM.DBUtility.Db db = new HZ.MBSM.DBUtility.Db(false);
                Grove.ORM.ObjectOperator oo = db.GetObjectOperator();
                HZ.MBSM.Model.Attorney Attorney = (HZ.MBSM.Model.Attorney)oo.Retrieve(typeof(HZ.MBSM.Model.Attorney), AccountId);
                HZ.MBSM.Model.Area Area = (HZ.MBSM.Model.Area)oo.Retrieve(typeof(HZ.MBSM.Model.Area), Attorney.AreaId);
                if (Attorney != null)
                {
                    AttorneyName.Text = Attorney.AttorneyName;
                    Address.Text = Attorney.Address;

                    if (Area != null)
                        this.Label1.Text = "<input  type='text' name='AreaName' style='width: 97px'  NotNull='地区名称' readonly value='" + Area.AreaName + "' /><input type='text' name='AreaId' style='display:none; width: 64px;' value='" + Attorney.AreaId + "'>";
                    else
                        this.Label1.Text = "<input  type='text' name='AreaName' style='width: 97px' NotNull='地区名称' /><input type='text' name='AreaId' style='display:none; width: 64px;' >";

                    txtUserCode.Text = SysFun.ToTrim(Attorney.UserCode);
                    Bank.Text =SysFun.ToTrim( Attorney.Bank);
                    Email.Text = SysFun.ToTrim(Attorney.Email);
                    EmployeeNum.Text = SysFun.ToTrim(Attorney.EmployeeNum.ToString());
                    Fax.Text = SysFun.ToTrim(Attorney.Fax);
                    if (Attorney.FoundDate > Convert.ToDateTime("1900-1-1"))
                        FoundDate.Text = SysFun.ToTrim(Attorney.FoundDate.ToShortDateString());
                    Fund.Text =SysFun.ToTrim( Attorney.Fund.ToString());
                    Lawer.Text =SysFun.ToTrim(Attorney.Lawer);
                    NationalCmp.Text = SysFun.ToTrim(Attorney.NationalCmp);
                    NetAddress.Text =SysFun.ToTrim( Attorney.NetAddress);
                    PostCode.Text = SysFun.ToTrim(Attorney.PostCode);                    
                    if (Attorney.registDate > Convert.ToDateTime("1900-1-1"))
                        RegistDate.Text = SysFun.ToTrim(Attorney.registDate.ToShortDateString());
                    RegistDate.Visible = false;
                    Telephone.Text = SysFun.ToTrim(Attorney.Telephone);
                }
            }
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        HZ.MBSM.DBUtility.Db db = new HZ.MBSM.DBUtility.Db(false);
        Grove.ORM.ObjectOperator oo = db.GetObjectOperator();

        HZ.MBSM.Model.Attorney Attorney = new HZ.MBSM.Model.Attorney();

        if (Attorney != null)
        {
            int rowCun = 0;
            Db db1 = new Db();
            System.Data.IDataReader dr;
            string strSql = "select count(accountid) as cun from dbo.v_account where ( dwname= '" + SysFun.ToTrim(AttorneyName.Text) + "' or usercode='" + SysFun.ToTrim(txtUserCode.Text) + "') and AccountId<>" + AccountId.ToString() ;
            dr = db1.DataReader(strSql);
            if (dr.Read())
            {
                rowCun = SysFun.ToInt(dr["cun"]);
            }
            dr.Close();
            dr.Dispose();
            db1.Close();
            if (rowCun > 0)
            {
                Response.Write("<script language=JavaScript>alert('该用户在数据库中已经存在！！！');</script>");
            }
            else
            {

                Attorney.AccountId = AccountId;
                Attorney.UserCode = txtUserCode.Text.ToString();
                Attorney.AttorneyName = AttorneyName.Text;
                Attorney.Address = Address.Text;
                Attorney.AreaId = SysFun.ToInt(Request.Form["AreaId"]);
                Attorney.Bank = Bank.Text;
                Attorney.Email = Email.Text;
                Attorney.EmployeeNum = SysFun.ToInt(EmployeeNum.Text);
                Attorney.Fax = Fax.Text;
                if (SysFun.ToTrim(FoundDate.Text) != "")
                    Attorney.FoundDate = Convert.ToDateTime(FoundDate.Text);
                Attorney.Fund = SysFun.ToInt(Fund.Text);
                Attorney.Lawer = Lawer.Text;
                Attorney.NationalCmp = NationalCmp.Text;
                Attorney.NetAddress = NetAddress.Text;
                Attorney.PostCode = PostCode.Text;
                if (SysFun.ToTrim(RegistDate.Text) != "")
                    Attorney.registDate = Convert.ToDateTime(RegistDate.Text);
                Attorney.Telephone = Telephone.Text;
                oo.Update(Attorney);
                HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "修改", "Account,Attorney", "oo.Update(Attorney)", "../Magage/AttoneyAdd.aspx");

                Response.Write("<script>alert('保存成功！！！');</script>");

                Response.Write("<script>window.opener.location.reload();window.close();</script>");
            }
        }

        oo.Dispose();
        db.Close();
    }

}
